import jieba
import random
import sys
import os
import time
import click
with open("%s"%sys.argv[1]) as f:
   text = f.read()
text = text.replace("\n","，")
reciteMode = input("模式：")
if not reciteMode:
    reciteMode = random.choice(["1","2","3"])

signs = ["，", "。", "！", "？", "“", "”", "《", "》", "（", "）","、","；",",","."," ","-","!",";","?","'"," "]

def replaceWord(x, s):
    res = []
    for i in x:
        if i in signs:
            res.append(i)
        elif ord(i) > 126:
            res.append(s * 2)
        else:
            res.append(s)
    return "".join(res)

def blank_print(blankIndex,words,delay):
    blankIndex2 = blankIndex.copy()
    p = ""
    for i in blankIndex:
        if i - 1 != p:
            w = words[i]
            words2 = words.copy()
            words2[i] = replaceWord(w, "_")
            for j in blankIndex2:
                if j != i:
                    words2[j] = replaceWord(words2[j], "_")
            text2 = "".join(words2)
            time.sleep(delay)
            click.clear()
            print(text2)
        blankIndex2.remove(i)
        p = i
    time.sleep(delay)
    click.clear()
    print(text)

def reciteMode1(text):
    words = jieba.lcut(text)
    l = 0
    for i in words:
        if not i in signs:
            l += 1
    l = l / 2
    blankIndex = []
    while l > 0:
        randomIndex = random.choice(range(len(words)))
        w = words[randomIndex]
        if (not w in signs) and (not randomIndex in blankIndex):
            blankIndex.append(randomIndex)
            l -= 1

    blankIndex.sort()
    blank_print(blankIndex,words,1)

def reciteMode2(text):
    words = jieba.lcut(text)
    signs = ["，", "。", "！", "？","；"]
    l = len(words)
    l0 = l
    keepIndex = [0]
    i = 0
    while l > 0:
        w = words[i]
        if w in signs:
            keepIndex.append(i)
            keepIndex.append(i + 1)
            if i+2 < l0:
                if words[i+1] == " ":
                    keepIndex.append(i + 2)
        i += 1
        l -= 1

    blankIndex = []
    for i in range(len(words)):
        if not i in keepIndex:
            blankIndex.append(i)

    blank_print(blankIndex,words,3)

def reciteMode3(text):
    words = jieba.lcut(text)
    signs = ["，", "。", "！", "？","；"]
    l = len(words)
    keepIndex = []
    i = 0
    flag = random.choice([True, False])
    while l > 0:
        w = words[i]
        if w in signs:
            flag = not (flag)
        if flag:
            keepIndex.append(i)
        i += 1
        l -= 1

    blankIndex = []
    for i in range(len(words)):
        if not i in keepIndex:
            blankIndex.append(i)

    blank_print(blankIndex,words,3)

eval("reciteMode" + reciteMode + "(text)")
time.sleep(2)
os.system("clear")
